package dynamicGui;

import gamePlay.Bet;
import gamePlay.Game;
import gamePlay.Guess;
import gamePlay.Lobby;
import gamePlay.Player;
import gui.MenuCycling;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import netcode.client.Client;
import netcode.helper.NetcodeHelper;

public class ResultsMenuE extends JPanel{
	Game game_ = MenuCyclingE.game_;
	Client client_ = MenuCyclingE.client_;
	int gameId = MenuCyclingE.game_.getGameID();
	
	
	String name = MenuCyclingE.game_.getPlayers().get(0).getPlayer_name();
	int permaChips = MenuCyclingE.game_.getPlayer().getPermanent_chips();
	
	/*
	 * Begin Constructors---------------------------------------------------
	 */
	//Default Constructor
	//Parameter Lobby is used to get the Map<Integer,Game> allGames;
    public ResultsMenuE() {
        super();
        System.out.println("Creating ResultsMenuE");
        

        this.setLayout(new GridLayout(0,1, 0, 10));
        addQuestionTextField();
        addAnswerTextField();
        addBestGuessTextField();
        addInAllResults();
        addNextRoundButtonToPane();
        addLeaveButton();
        addFAQButton();
    }
    /*
	 * End Constructors------------------------------------------------------------------------------------------------
	 */
    
    
    
    /*
     * Begin Text Creation Methods---------------------------------------------------------------------------------
     */
    
    private void addQuestionTextField(){
    	//displays the original question asked
    	JLabel questionTextField = new JLabel("Question: " +MenuCyclingE.getQuestion());
    	this.add(questionTextField);
    }
    
    private void addAnswerTextField(){
    	//displays the answer to the original question asked
    	JLabel answerTextField = new JLabel("Answer: " +MenuCyclingE.getAnswer());
    	this.add(answerTextField);
    }
    
    private void addBestGuessTextField(){
    	//displays the best answer that was given
    	JLabel bestGuessTextField = new JLabel("Best Guess: " +MenuCyclingE.getBest());
    	this.add(bestGuessTextField);
    }
    
    //diplay all the answers given to the players
    private void addInAllResults(){
    	/*creates 3 labels for every player
    	 * 1) Players Name
    	 * 2) Total Chips
    	 * 3) Payout //err... apparently they have it in the old gui but never actually implemented it
    	 * 
    	 */
    	
    	JPanel playersResultsPanel = new JPanel();
    	playersResultsPanel.setLayout(new GridLayout(0,2, 0, 10)); // change this to playersResultsPanel.setLayout(new GridLayout(0,3, 0, 10)); if we get the third label working
    	
    	//run through all the players to create all the labels
    	ArrayList<Player> playerArrayList = MenuCyclingE.game_.getPlayers();
    	Iterator<Player> iterator = playerArrayList.iterator();
    	while (iterator.hasNext()) {
    		Player currentPlayerInArrayList = iterator.next();
    		
    		//Player Name
    		JLabel playerLabel = new JLabel(currentPlayerInArrayList.getPlayer_name());
    		playersResultsPanel.add(playerLabel);
        	
        	//Total Chips
        	JLabel totalChipsLabel = new JLabel("Total Chips: " +currentPlayerInArrayList.getPlayerChips());
        	playersResultsPanel.add(totalChipsLabel);
        	
        	//not implemented?
//        	JLabel payoutLabel = new JLabel("Payout: " +currentPlayerInArrayList.);
//        	this.add(payoutLabel);
    	}
    	
    	//finnaly add this seperate panal to the main one
    	this.add(playersResultsPanel);
    }
    
    
	//Add FAQButton
	private void addFAQButton(){
		JButton refreshButton = new JButton("FAQ");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	FAQButtonActionPerformed(evt);
            }
        });
	}
    /*
     * END Text Creation Methods---------------------------------------------------------------------------------
     */

    
    
    
    
    
    
    /*Begin button Creation Methods---------------------------------------------------------------------------------
     * 
     */
    private void addNextRoundButtonToPane(){
    	//create the ok button and add it
        JButton betButton = new JButton("Ok");
        betButton.setPreferredSize(new Dimension(200, 30));
        this.add(betButton);
        betButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	nextRoundButtonActionPerformed(evt);
            }
        });
    }
    
	//Add LeaveButton
	private void addLeaveButton(){
		JButton refreshButton = new JButton("LeaveCurrentGame");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	leaveButtonActionPerformed(evt);
            }
        });
	}
    /*
     * End button Creation Methods------------------------------------------------------------------------------------------
     */
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*
     *ActionListeners begin--------------------------------------------------------------------------------------------------
     */
	//Pressing leavegame Button
	private void leaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
		client_.sendLeaveGameRequest(MenuCyclingE.game_.getGameID());
		//goes back to the lobby selection menu
		MenuCyclingE.displayLobbySelectionMenu();
	}
	
	
	private void nextRoundButtonActionPerformed(java.awt.event.ActionEvent evt) {
		//set status to ready
		System.out.println("DEBUG: settting to ready, waiting until game starts");
		MenuCyclingE.displayQuestionMenu();
	}
	
	
	//Pressing FAQ Button
	private void FAQButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuickGameButtonActionPerformed
		try {
	        //Set your page url in this string. For eg, I m using URL for Google Search engine
	        String url = "http://homepages.uwp.edu/dalby001/faq.html";
	        java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
	      }
	      catch (java.io.IOException e) {
	          System.out.println(e.getMessage());
	      }
	}
    /*
     *ActionListeners end--------------------------------------------------------------------------------------------------
     */

    
    
    
}
